package com.dashunzi.yueyutianqi.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dashunzi.yueyutianqi.Bean.City;
import com.dashunzi.yueyutianqi.Bean.County;
import com.dashunzi.yueyutianqi.Bean.Province;

import java.util.ArrayList;
import java.util.List;

/**
 * 常用的数据库操作
 * Created by Administrator on 2016/4/9 0009.
 * 作者：大顺子
 */
public class YueYuWeatherDB {
    /**
     * 数据库名字
     */
    public static final String DB_NAME = "yueyu_Weather";
    /**
     * 数据库版本
     */
    public static final int DB_VERSION = 1;
    private static YueYuWeatherDB yueYuWeatherDB;
    private SQLiteDatabase mDb;

    /**
     * 将构造方法私有化
     */
    private YueYuWeatherDB(Context context) {
        YueYuWeatherOpenHelper yueYuWeatherOpenHelper = new YueYuWeatherOpenHelper(
                context, DB_NAME, null, DB_VERSION);
        mDb = yueYuWeatherOpenHelper.getWritableDatabase();
    }

    /**
     * 获取YueYuWeather的实例
     */
    public synchronized static YueYuWeatherDB getInstance(Context context) {
        if (yueYuWeatherDB == null) {
            yueYuWeatherDB = new YueYuWeatherDB(context);
        }
        return yueYuWeatherDB;
    }

    /**
     * 将province实例存取到数据库中
     */
    public void saveProvince(Province province) {
        if (province != null) {
            ContentValues values = new ContentValues();
            values.put("province_name", province.getProvinceName());
            values.put("province_code", province.getProvinceCode());
            mDb.insertOrThrow("Province", null, values);
        }
    }

    /**
     * 从数据库中读取全国所以省份的信息
     */
    public List<Province> loadProvinces() {
        //创建一个集合用来存放全国的所以省份对象
        List<Province> provinceList = new ArrayList<>();
        Cursor cursor = mDb.query("Province", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            Province province = new Province();
            province.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
            province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));
            provinceList.add(province);
        }
        if (cursor != null) {
            cursor.close();
        }
        return provinceList;
    }

    /**
     * 将city实例存储到数据库中
     */
    public void saveCity(City city) {
        if (city != null) {
            ContentValues values = new ContentValues();
            values.put("city_name", city.getCityName());
            values.put("city_code", city.getCityCode());
            values.put("province_id", city.getProvinceId());
            mDb.insert("City", null, values);
        }
    }

    /**
     * 从数据库中读取某省下所有的城市信息
     */
    public List<City> loadCities(int provinceId) {
        List<City> cityList = new ArrayList<>();
        Cursor cursor = mDb.query("City", null, "province_id=?", new String[]{String.valueOf(provinceId)}, null, null, null);
        while (cursor.moveToNext()) {
            City city = new City();
            city.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            city.setCityName(cursor.getString(cursor
                    .getColumnIndex("city_name")));
            city.setCityCode(cursor.getString(cursor
                    .getColumnIndex("city_code")));
            city.setProvinceId(provinceId);
            cityList.add(city);
        }

        return cityList;
    }

    /**
     * 将County实例存储到数据库。
     */
    public void saveCounty(County county) {
        if (county != null) {
            ContentValues values = new ContentValues();
            values.put("county_name", county.getCountyName());
            values.put("county_code", county.getCountyCode());
            values.put("city_id", county.getCityId());
            mDb.insert("County", null, values);
        }
    }

    /**
     * 从数据库读取某城市下所有的县信息。
     */
    public List<County> loadCounties(int cityId) {
        List<County> countyList = new ArrayList<County>();
        Cursor cursor = mDb.query("County", null, "city_id = ?",
                new String[]{String.valueOf(cityId)}, null, null, null);
        while (cursor.moveToNext()) {
            County county = new County();
            county.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            county.setCountyName(cursor.getString(cursor
                    .getColumnIndex("county_name")));
            county.setCountyCode(cursor.getString(cursor
                    .getColumnIndex("county_code")));
            county.setCityId(cityId);
            countyList.add(county);

        }
        return countyList;
    }

}